﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controller;
using Model.User;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void RegisterButton_Click(object sender, EventArgs e)
    {
        LabelError.Visible = false;
        bool tryLogin = true;

        tryLogin = IfIsEmptySetVisible(TextBoxFirstName, LabelErrorFirstName) && tryLogin;
        tryLogin = IfIsEmptySetVisible(TextBoxLastName, LabelErrorLastName) && tryLogin;
        tryLogin = IfIsEmptySetVisible(TextBoxEmail, LabelErrorEmail) && tryLogin;
        tryLogin = IfIsEmptySetVisible(TextBoxPassword, LabelErrorPassword) && tryLogin;

        if (tryLogin)
        {
            if (TextBoxEmail.Text.IndexOf("@uj.edu.pl") <= 0)
            {
                LabelError.Visible = true;
                return;
            }

            Person p = AdministrationController.register(TextBoxFirstName.Text, TextBoxLastName.Text, 
                                                         TextBoxEmail.Text, TextBoxPassword.Text);

            if (p != null)
            {
                Session["Person"] = p.getId();
                Session["LastAccess"] = DateTime.Now.ToString();
                Response.Redirect("~/ViewCoursesLogged.aspx");
            }
            else
            {
                LabelError.Text = "Coś poszło nie tak spróbuj ponownie później.";
            }
        }
    }

    bool IfIsEmptySetVisible(TextBox t, Label l)
    {
        if (t.Text.Replace(" ","a") == "")
        {
            l.Visible = true;
            return false;
        }
        else
            l.Visible = false;

        return true;
    }
}